import request from './request'
import type { 
  BodyRecord, BodyRecordForm, 
  EatingRecord, EatingRecordForm,
  SleepRecord, SleepRecordForm,
  SportsRecord, SportsRecordForm,
  HealthQueryParams, HealthListResponse 
} from '@/types/health'

// 帖子类型定义
export interface Post {
  id: number
  title: string
  content: string
  user_id: number
  user_nickname: string
  user_avatar: string
  comments_count: number
  status: string
  status_display: string
  created_at: string
  updated_at: string
  is_favorited?: boolean
  like_type?: number | null
}

export interface PostFavorite {
  id: number
  post: number
  post_title: string
  post_user_nickname: string
  created_at: string
}

export interface PostStats {
  favorites_count: number
  likes_count: number
  dislikes_count: number
}

export interface PostUserStatus {
  is_favorited: boolean
  like_type: number | null
}

// 健康资讯类型定义
export interface HealthNews {
  id: number
  title: string
  url: string
  publish_time: string
  title_image: string
  website: string
  hash_value: string
  created_at: string
  updated_at: string
}

export interface HealthNewsListResponse {
  count: number
  next: string | null
  previous: string | null
  results: HealthNews[]
}

export interface HealthNewsQueryParams {
  page?: number
  page_size?: number
}

// 用户健康数据统计类型定义
export interface UserHealthStats {
  bodyRecordCount: number
  eatingRecordCount: number
  sleepRecordCount: number
  sportsRecordCount: number
}

// 仪表板统计数据类型定义
export interface DashboardStats {
  userCount: number
  bodyRecordCount: number
  eatingRecordCount: number
  sleepRecordCount: number
  sportsRecordCount: number

}

// 身体指标记录API
export function getBodyRecords(params: HealthQueryParams) {
  return request<HealthListResponse<BodyRecord>>({ 
    url: '/health/body-records/',
    method: 'get',
    params
  })
}

// 获取身体指标记录用于图表展示
export function getBodyRecordsForChart(params: HealthQueryParams) {
  return request<HealthListResponse<BodyRecord>>({ 
    url: '/health/body-records/',
    method: 'get',
    params: {
      ...params,
      page_size: 30 // 获取最近30条记录用于图表展示
    }
  })
}

export function createBodyRecord(data: BodyRecordForm) {
  return request<BodyRecord>({ 
    url: '/health/body-records/',
    method: 'post',
    data
  })
}

export function updateBodyRecord(id: number, data: BodyRecordForm) {
  return request<BodyRecord>({ 
    url: `/health/body-records/${id}/`,
    method: 'put',
    data
  })
}

export function deleteBodyRecord(id: number) {
  return request({ 
    url: `/health/body-records/${id}/`,
    method: 'delete'
  })
}

// 饮食记录API
export function getEatingRecords(params: HealthQueryParams & { meal_type?: string }) {
  return request<HealthListResponse<EatingRecord>>({
    url: '/health/eating-records/',
    method: 'get',
    params
  })
}

// 获取饮食记录用于图表展示
export function getEatingRecordsForChart(params: HealthQueryParams) {
  return request<HealthListResponse<EatingRecord>>({ 
    url: '/health/eating-records/',
    method: 'get',
    params: {
      ...params,
      page_size: 30 // 获取最近30条记录用于图表展示
    }
  })
}

export function createEatingRecord(data: EatingRecordForm) {
  return request<EatingRecord>({ 
    url: '/health/eating-records/',
    method: 'post',
    data
  })
}

export function updateEatingRecord(id: number, data: EatingRecordForm) {
  return request<EatingRecord>({ 
    url: `/health/eating-records/${id}/`,
    method: 'put',
    data
  })
}

export function deleteEatingRecord(id: number) {
  return request({ 
    url: `/health/eating-records/${id}/`,
    method: 'delete'
  })
}

// 睡眠记录API
export function getSleepRecords(params: HealthQueryParams) {
  return request<HealthListResponse<SleepRecord>>({ 
    url: '/health/sleep-records/',
    method: 'get',
    params
  })
}

export function createSleepRecord(data: SleepRecordForm) {
  return request<SleepRecord>({ 
    url: '/health/sleep-records/',
    method: 'post',
    data
  })
}

export function updateSleepRecord(id: number, data: SleepRecordForm) {
  return request<SleepRecord>({ 
    url: `/health/sleep-records/${id}/`,
    method: 'put',
    data
  })
}

export function deleteSleepRecord(id: number) {
  return request({ 
    url: `/health/sleep-records/${id}/`,
    method: 'delete'
  })
}

// 运动记录API
export function getSportsRecords(params: HealthQueryParams & { sport_type?: string }) {
  return request<HealthListResponse<SportsRecord>>({
    url: '/health/sports-records/',
    method: 'get',
    params
  })
}

export function createSportsRecord(data: SportsRecordForm) {
  return request<SportsRecord>({ 
    url: '/health/sports-records/',
    method: 'post',
    data
  })
}

export function updateSportsRecord(id: number, data: SportsRecordForm) {
  return request<SportsRecord>({ 
    url: `/health/sports-records/${id}/`,
    method: 'put',
    data
  })
}

export function deleteSportsRecord(id: number) {
  return request({ 
    url: `/health/sports-records/${id}/`,
    method: 'delete'
  })
}

// 帖子API
export function getPublicPosts(params: any) {
  return request<HealthListResponse<Post>>({
    url: '/health/public-posts/',
    method: 'get',
    params
  })
}

export function getPostDetail(id: number) {
  return request<Post>({ 
    url: `/health/posts/${id}/`,
    method: 'get'
  })
}

export function favoritePost(postId: number) {
  return request({ 
    url: `/health/posts/${postId}/favorite/`,
    method: 'post'
  })
}

export function unfavoritePost(postId: number) {
  return request({ 
    url: `/health/posts/${postId}/unfavorite/`,
    method: 'delete'
  })
}

export function likePost(postId: number, likeType: number) {
  return request({ 
    url: `/health/posts/${postId}/like/`,
    method: 'post',
    data: { like_type: likeType }
  })
}

export function unlikePost(postId: number) {
  return request({ 
    url: `/health/posts/${postId}/unlike/`,
    method: 'delete'
  })
}

export function getPostStats(postId: number) {
  return request<PostStats>({ 
    url: `/health/posts/${postId}/stats/`,
    method: 'get'
  })
}

export function getPostUserStatus(postId: number) {
  return request<PostUserStatus>({ 
    url: `/health/posts/${postId}/status/`,
    method: 'get'
  })
}

export function getFavoritePosts(params: any) {
  return request<HealthListResponse<PostFavorite>>({ 
    url: '/health/favorites/',
    method: 'get',
    params
  })
}

// 用户健康数据统计API
export function getUserHealthStats() {
  return request<UserHealthStats>({ 
    url: '/health/user/stats/',
    method: 'get'
  })
}

// 仪表板统计数据API
export function getDashboardStats() {
  return request<DashboardStats>({ 
    url: '/health/admin/stats/',
    method: 'get'
  })
}

// 健康资讯API
export function getHealthNews(params: HealthNewsQueryParams) {
  return request<HealthNewsListResponse>({ 
    url: '/health/health-news/',
    method: 'get',
    params,
    noAuth: true  // 健康资讯接口不需要认证
  } as any)
}

// 获取用户昨日健康分析
export const getUserYesterdayAnalysis = () => {
  return request.get('/health/user/yesterday-analysis/')
}
